package tau.dimes.util;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ProgressMap
{
	private static final Map<String, Integer> progressMap;
	private static final ReadWriteLock progressMapLock;
	
	static
	{
		progressMap = new HashMap<String, Integer>();
		progressMapLock = new ReentrantReadWriteLock();
	}

	private ProgressMap()
	{
	}
	
	public static void add(String requestID)
	{
		progressMapLock.writeLock().lock();
		progressMap.put(requestID, 0);
		progressMapLock.writeLock().unlock();
	}
	
	public static void remove(String requestID)
	{
		progressMapLock.writeLock().lock();
		progressMap.remove(requestID);
		progressMapLock.writeLock().unlock();
	}
	
	public static boolean contains(String requestID)
	{
		boolean result;
		
		progressMapLock.readLock().lock();
		result = progressMap.containsKey(requestID);
		progressMapLock.readLock().unlock();
		
		return result;
	}
	
	public static void set(String requestID, int value)
	{
		progressMapLock.writeLock().lock();
		progressMap.put(requestID, value);
		progressMapLock.writeLock().unlock();
	}
	
	public static Integer get(String requestID)
	{
		Integer result = null;
		
		progressMapLock.readLock().lock();
		result = progressMap.get(requestID);
		progressMapLock.readLock().unlock();
		
		return result;
	}
}
